# Using OpenAI functions

This walkthrough demonstrates how to incorporate OpenAI function-calling API's in a chain. We'll go over:

1. How to use functions to get structured outputs from ChatOpenAI
2. How to create a generic chain that uses (multiple) functions
3. How to create a chain that actually executes the chosen function

## Getting structured outputs

We can take advantage of OpenAI functions to try and force the model to return a particular kind of structured output.
We'll use `createStructuredOutputRunnable` to create our chain, which takes the desired structured output as a valid JSONSchema.

import JSONSchemaExample from "@examples/chains/openai_functions_json_schema.ts";
import CodeBlock from "@theme/CodeBlock";

<CodeBlock language="typescript">{JSONSchemaExample}</CodeBlock>

:::tip
You can see a LangSmith trace of this example [here](https://smith.langchain.com/public/c478790b-0b47-467f-894c-bc95b9b861a5/r).
:::

## Creating a generic OpenAI functions chain

To create a generic OpenAI functions chain, we can use the `createOpenaiFnRunnable` method.
This is the same as `createStructuredOutputRunnable` except that instead of taking a single output schema, it takes a sequence of function definitions.

import OpenAIFnExample from "@examples/chains/openai_functions_runnable.ts";

<CodeBlock language="typescript">{OpenAIFnExample}</CodeBlock>

:::tip
You can see a LangSmith trace of this example [here](https://smith.langchain.com/public/b042c342-da49-466c-afee-f5604b2fa232/r).
:::

## Multiple functions

import OpenAIMultiFnExample from "@examples/chains/openai_multi_functions_runnable.ts";

<CodeBlock language="typescript">{OpenAIMultiFnExample}</CodeBlock>

:::tip
You can see a LangSmith trace of this example [here](https://smith.langchain.com/public/06550907-1df5-4279-8ac5-00acb5e56ea7/r).
:::
